﻿using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SlamDunk.MES.IAppService.DTOs.Outputs.PlanContext;
using SlamDunk.MES.IAppService.Interfaces;
using SlamDunk.MES.Presentation.Maps;
using SlamDunk.MES.Presentation.Models.NotifyModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.ServiceModel.Extensions;
using System.Threading.Tasks;

namespace SlamDunk.MES.Client.ViewModels.SaleOrder
{
    /// <summary>
    /// 销售订单审核视图模型
    /// </summary>
    public class CheckViewModel : ScreenBase
    {
        #region # 字段及构造器

        /// <summary>
        /// 计划管理服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IPlanContract> _planContract;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public CheckViewModel(ServiceProxy<IPlanContract> planContract)
        {
            this._planContract = planContract;
        }

        #endregion

        #region # 属性

        #region 销售订单Id —— Guid SaleOrderId
        /// <summary>
        /// 销售订单Id
        /// </summary>
        public Guid SaleOrderId { get; set; }
        #endregion

        #region 销售订单编号 —— string SaleOrderNo
        /// <summary>
        /// 销售订单编号
        /// </summary>
        public string SaleOrderNo { get; set; }
        #endregion

        #region 销售订单名称 —— string SaleOrderName
        /// <summary>
        /// 销售订单名称
        /// </summary>
        public string SaleOrderName { get; set; }
        #endregion

        #region 客户名称 —— string CustomerName
        /// <summary>
        /// 客户名称
        /// </summary>
        public string CustomerName { get; set; }
        #endregion

        #region 下单日期 —— DateTime? IssuedDate
        /// <summary>
        /// 下单日期
        /// </summary>
        public DateTime? IssuedDate { get; set; }
        #endregion

        #region 交付日期 —— DateTime? DeliveryDate
        /// <summary>
        /// 交付日期
        /// </summary>
        public DateTime? DeliveryDate { get; set; }
        #endregion

        #region 描述 —— string Description
        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; set; }
        #endregion

        #region 销售订单明细列表 —— ObservableCollection<SaleOrderDetail> Details
        /// <summary>
        /// 销售订单明细列表
        /// </summary>
        public ObservableCollection<SaleOrderDetailNm> Details { get; set; }
        #endregion

        #region 审核意见 —— string CheckedRemark
        /// <summary>
        /// 审核意见
        /// </summary>
        [DependencyProperty]
        public string CheckedRemark { get; set; }
        #endregion

        #endregion

        #region # 方法

        //Initializations

        #region 加载 —— void Load(SaleOrderInfo saleOrder...
        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="saleOrder">销售订单</param>
        /// <param name="saleOrderDetails">销售订单明细集</param>
        public void Load(SaleOrderInfo saleOrder, IEnumerable<SaleOrderDetailInfo> saleOrderDetails)
        {
            this.SaleOrderId = saleOrder.Id;
            this.SaleOrderNo = saleOrder.Number;
            this.SaleOrderName = saleOrder.Name;
            this.CustomerName = saleOrder.CustomerInfo.Name;
            this.IssuedDate = saleOrder.IssuedDate;
            this.DeliveryDate = saleOrder.DeliveryDate;
            this.Description = Description;
            IEnumerable<SaleOrderDetailNm> detailNms = saleOrderDetails.Select(x => x.ToNotifyView());
            this.Details = new ObservableCollection<SaleOrderDetailNm>(detailNms);
        }
        #endregion


        //Actions

        #region 通过 —— async void Approve()
        /// <summary>
        /// 通过
        /// </summary>
        public async void Approve()
        {
            this.Busy();

            await Task.Run(() => this._planContract.Channel.CheckSaleOrder(this.SaleOrderId, true, this.CheckedRemark));
            await base.TryCloseAsync(true);

            this.Idle();
        }
        #endregion

        #region 拒绝 —— async void Reject()
        /// <summary>
        /// 拒绝
        /// </summary>
        public async void Reject()
        {
            this.Busy();

            await Task.Run(() => this._planContract.Channel.CheckSaleOrder(this.SaleOrderId, false, this.CheckedRemark));
            await base.TryCloseAsync(true);

            this.Idle();
        }
        #endregion

        #endregion
    }
}
